﻿#include "add.h"
#include "ui_add.h"
#include<QFile>
#include<QDebug>
#include<QMessageBox>
#include<QPushButton>
#include<QList>
#include<QAbstractButton>
#include<QFile>
#include<QIODevice>
#include<QTextStream>
#include<QtSql/QSqlDatabase>
#include<QtSql/QSqlQuery>
add::add(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::add)
{
    ui->setupUi(this);
    setWindowTitle("增");
    QFile file("../add.css");
    file.open(QFile::ReadOnly);
    if(file.isOpen()){
        QString str=QLatin1String(file.readAll());
        setStyleSheet(str);
    }else{
        qDebug()<<"open file fail";
    }
    file.close();
}

add::~add()
{
    delete ui;
}

void add::clearContent()
{
    ui->nameEdit->clear();
    ui->idEdit->clear();
    ui->man->setChecked(true);
    ui->ageCombo->setCurrentIndex(0);
    ui->deptCombo->setCurrentIndex(0);
    QList<QAbstractButton*>funBtn=ui->funGroup->buttons();
    for(int i=0;i<funBtn.length();i++){
        funBtn[i]->setChecked(false);
    }
    ui->nameEdit->setFocus();
    ui->nameVerify->clear();
    ui->idVerify->clear();
    ui->nameEdit->setStyleSheet("border:none");
    ui->idEdit->setStyleSheet("border:none");
}

void add::writeFile(QString str)
{
    QFile file("stuInfo.txt");
          if (!file.open(QIODevice::Append | QIODevice::Text)){
              QMessageBox::critical(this,"增","写入出错,添加失败!","确定");
              return;
          }
          QTextStream out(&file);
          out << str;
          QMessageBox::information(this,"增","添加成功!","确定");
}

bool add::connectMysql()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setDatabaseName("stuinfo");
    db.setHostName("localhost");
    db.setUserName("root");
    db.setPassword("root");
    db.setPort(3306);
    if(db.open())
        return true;
    return false;
}

void add::on_okBtn_clicked()
{
    QString name=ui->nameEdit->text();
    QString id=ui->idEdit->text();
    QString sex=ui->sexGroup->checkedButton()->text();
    QString age=ui->ageCombo->currentText();
    QString dept=ui->deptCombo->currentText();
    QString fun="";
    QList<QAbstractButton*>funArr=ui->funGroup->buttons();
    for(int i=0;i<funArr.length();i++){
        if(funArr[i]->isChecked())
            fun+=funArr[i]->text()+" ";
    }
    QString content="姓名: "+name+"\n"+"学号: "+id+"\n性别: "+sex+"\n年龄: "+age+"\n院系: "+dept+"\n爱好: "+fun;
    QString fileStr=name+" "+id+" "+sex+" "+age+" "+dept+" "+fun+"\n";
    if(name.length()<1||id.length()<4||name.contains(" ")||id.contains(" ")){
        QMessageBox::critical(this,"增","信息有误!","确认");
    }else{
        QMessageBox msgBox(this);
        msgBox.setWindowTitle("增");
          msgBox.setText("学生信息");
          msgBox.setInformativeText(content);
          QPushButton*okBtn=msgBox.addButton("确认",QMessageBox::AcceptRole);
          QPushButton*cancelBtn=msgBox.addButton("取消",QMessageBox::RejectRole);
          msgBox.setDefaultButton(okBtn);
          int ret = msgBox.exec();
          if(ret==0){
             // writeFile(fileStr);
              if(!connectMysql()){
                  QMessageBox::critical(this,"增","数据库连接失败,添加失败!","确定");
              }else{

                   QString ret=QString("insert into student values('%1',%2,'%3',%4,'%5','%6')").arg(name)
                           .arg(id).arg(sex).arg(age).arg(dept).arg(fun);
                   QSqlQuery addStu(ret);
                   if(addStu.numRowsAffected()>0){
                       QMessageBox::information(this,"增","添加成功!","确认");
                   }else{
                        QMessageBox::critical(this,"增","未知错误,添加失败!","确定");
                   }
              }

          }
          qDebug()<<ret;
    }

}

void add::on_nameEdit_textEdited(const QString &arg1)
{
    if(arg1.length()<1){
        ui->nameEdit->setStyleSheet("border:1px solid red");
        ui->nameVerify->setStyleSheet("color:red");
        ui->nameVerify->setText("姓名不能为空!");
    }
    else if(arg1.contains(" ")){
        ui->nameEdit->setStyleSheet("border:1px solid red");
        ui->nameVerify->setStyleSheet("color:red");
        ui->nameVerify->setText("不能包含空格!");
    }
    else{
        ui->nameEdit->setStyleSheet("border:1px solid dodgerblue");
        ui->nameVerify->setStyleSheet("color:green");
        ui->nameVerify->setText("合法的用户名");
    }
}

void add::on_nameEdit_editingFinished()
{
    if(ui->nameEdit->text().length()<1){
        ui->nameEdit->setStyleSheet("border:1px solid red");
        ui->nameVerify->setStyleSheet("color:red");
        ui->nameVerify->setText("姓名不能为空!");
    }
}

void add::on_idEdit_textEdited(const QString &arg1)
{
    if(arg1.length()<4){
        ui->idEdit->setStyleSheet("border:1px solid red");
        ui->idVerify->setStyleSheet("color:red");
        ui->idVerify->setText("不能少于4位!");
    }
    else if(arg1.contains(" ")){
        ui->idEdit->setStyleSheet("border:1px solid red");
        ui->idVerify->setStyleSheet("color:red");
        ui->idVerify->setText("不能包含空格!");
    }
    else{
        ui->idEdit->setStyleSheet("border:1px solid dodgerblue");
        ui->idVerify->setStyleSheet("color:green");
        ui->idVerify->setText("合法的学号");
    }
}

void add::on_idEdit_editingFinished()
{
    if(ui->idEdit->text().length()<1){
        ui->idEdit->setStyleSheet("border:1px solid red");
        ui->idVerify->setStyleSheet("color:red");
        ui->idVerify->setText("学号不能为空!");
    }
}

void add::on_cancelBtn_clicked()
{
    this->close();
    clearContent();
}
